package leju.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ListView;

public class HorizontalSlideDeleteListView extends ListView{

	private int HRIZONTAL_TOUCH_SLOP;
	private final int MAX_TIME_SLOP = 200;
	
	private HorizontalSlideCallback callback;
	
	public HorizontalSlideDeleteListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		HRIZONTAL_TOUCH_SLOP = ViewConfiguration.getTouchSlop();
	}

	float lastX, lastY;
	boolean isHorizontal = false; 
	//滑动过则视为正在滑动状态
	boolean isHorizontaling = false;
	
	private long lastMotionTime;
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		
		switch(action){
		case MotionEvent.ACTION_DOWN:
			lastX = ev.getX();
			lastY = ev.getY();
			isHorizontal = false;
			lastMotionTime = System.currentTimeMillis();
			return true;
		case MotionEvent.ACTION_MOVE:
			float deltX = lastX - ev.getX();
			float deltY = lastY - ev.getY();
			long curMotionTime = System.currentTimeMillis();
			if(isHorizontaling && !isHorizontal){
				isHorizontal = true;
				if(null != callback){
					callback.horizontalSlideCallback(SlideMode.HIDE, getPositionByY((int)ev.getX(), (int)ev.getY()));
				}
			}else if(!isHorizontal 
					&& deltX !=0
					&& Math.abs(deltX) > 10*Math.abs(deltY)
					&& (Math.abs(deltX) > HRIZONTAL_TOUCH_SLOP)){
				if(isPressed())setPressed(false);
				int position = getPositionByY((int)ev.getX(), (int)ev.getY());
				View childView = getChildAt(position);
				if(null != childView && childView.isPressed()) childView.setPressed(false);
				
				if(null != callback){
					if(deltX > 0)	callback.horizontalSlideCallback(SlideMode.RIGHT, getPositionByY((int)ev.getX(), (int)ev.getY()));
					if(deltX < 0)	callback.horizontalSlideCallback(SlideMode.LEFT, getPositionByY((int)ev.getX(), (int)ev.getY()));
				}
				//isHorizontaling = true;
				isHorizontal = true;
			}else if(!isHorizontal 
					&& Math.abs(deltX) < 3*Math.abs(deltY)
					&& Math.abs(deltY) < HRIZONTAL_TOUCH_SLOP
					&& (curMotionTime - lastMotionTime) >MAX_TIME_SLOP){
				View childView = getChildAt(getPositionByY((int)ev.getX(), (int)ev.getY()));
				if(null != childView) childView.setPressed(true);
			}
			
			if(isHorizontal) return true;
			
			break;
		case MotionEvent.ACTION_UP:
			setPressed(false);
			View childView = getChildAt(getPositionByY((int)ev.getX(), (int)ev.getY()));
			if(null != childView) childView.setPressed(false);
			if(isHorizontal || isHorizontaling){
				if(!isHorizontaling) isHorizontaling = true;
				else  isHorizontaling = false;
				return true;
			}
			break;
		}
		
		return super.onTouchEvent(ev);
	}

	private int getPositionByY(int x, int y){
		/*int childCount = getChildCount();
		for (int i = childCount - 1; i >= 0; i--) {
            View v = getChildAt(i);
            if (y >= v.getTop()) {
                return getFirstVisiblePosition() + i;
            }
        }
		return INVALID_POSITION;*/
		return pointToPosition(x, y);
	}
	
	public enum SlideMode{
		LEFT, RIGHT, HIDE, DEFAULT;
	}
	
	public void setHorizontalSlideCallback(HorizontalSlideCallback callback){
		this.callback = callback;
	}
	
	public interface HorizontalSlideCallback{
		void horizontalSlideCallback(SlideMode direction, int position);
	}
}
